﻿using FoodModel;
using J.FoodManageGet.Application.Command.GroupingCommand;
using J.FoodRepository.InterFace;
using MediatR;

namespace J.FoodManageGet.Application.CommandHandler.GroupingCommandHandler
{
    public class GroupingCommandHandler : IRequestHandler<GroupingCommand, List<GroupingDto>>
    {
        private readonly IDishesReposittory _dishesReposittory;
        private readonly IGroupingRepository _groupingRepository;

        public GroupingCommandHandler(IDishesReposittory dishesReposittory, IGroupingRepository groupingRepository)
        {
            _dishesReposittory = dishesReposittory;
            _groupingRepository = groupingRepository;
        }

        public async Task<List<GroupingDto>> Handle(GroupingCommand request, CancellationToken cancellationToken)
        {
            var list = await _groupingRepository.GetAllAsync();
            var list1 = await _dishesReposittory.GetAllAsync();

            var info = (from a in list
                        join b in list1 on a.GroupingId equals b.GroupingId
                        where (a.GroupingId == request.GroupingId)
                        select new GroupingDto
                        {
                            DishesName = b.DishesName,
                            DishId = b.DishId,
                            GroupingId = a.GroupingId,
                            GroupingName = a.GroupingName,
                            GroupingNum = a.GroupingNum,
                        }).ToList();
            return info;
        }
    }
}
